library(tidyverse)
library(tidycensus)
library(tm)
library(openintro)
library(scales)
library(maps)
library(mapproj)
library(usmap)

## Loading county map and adding FIPS codes
county_map = us_map("county") %>%
  mutate(fips = str_remove(fips, "^0+"))

## Load delivery data
load("cnty_analysis.RData")

boxes = cnty_analysis %>%
  mutate(boxes = factor(ntile(boxes_pc, 5))) %>%
  select(fips, boxes)

## Plot
boxes_map = plot_usmap(regions = "county", data = boxes, values = "boxes", color = "black") +
  scale_fill_brewer(palette = "YlGnBu", labels = c("Q1", "Q2", "Q3", "Q4",
                                                   "Q5")) +
  theme_classic() +
  labs(y = NULL, x = NULL,
       fill = NULL) +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        axis.line.x = element_blank(),
        axis.line.y = element_blank(),
        legend.position = c(.8, 0.1)) +
  guides(fill = guide_legend(nrow = 1))
